﻿#----------------------------------------
#---- longtdbk@gmail.com-----------------
#----------------------------------------

import cgi
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users

#--------------------
#define Entities
#--------------------

class VaiTro(db.Model):
    TenVaiTro = db.StringProperty(required=True)

class QuiMo(db.Model):
    SoLuong = db.StringProperty(required=True)

class MucLuong(db.Model):
    TenMucLuong = db.StringProperty(required=True)

class NguoiDung(db.Model):
    TenNguoiDung = db.UserProperty()
    UngDung  = db.StringProperty()
    VaiTro = db.ReferenceProperty(VaiTro,required=True,collection_name = 'ref_VaiTro_ND')
    
class TinhThanh(db.Model):
    TenTinhThanh = db.StringProperty(required=True)

class LinhVuc(db.Model):
    TenLinhVuc = db.StringProperty(required=True)
    #NguoiDung = db.ReferenceProperty(NguoiDung,required=True,collection_name='ref_NguoiDung_LV')

class LoaiHinhCongViec(db.Model):
    TenLoaiHinh = db.StringProperty(required=True)

class NganhNghe(db.Model):    
    TenNganhNghe = db.StringProperty(required=True)
    LinhVucID = db.ReferenceProperty(LinhVuc)
class NganhNghe_NguoiDung(db.Model):
    NganhNgheID = db.ReferenceProperty(NganhNghe,required=True,collection_name='ref_NganhNghe_NNND')
    NguoiDung = db.ReferenceProperty(NguoiDung,required=True,collection_name='ref_NguoiDung_NNND')
class CapBac(db.Model):
    TenCapBac = db.StringProperty(required = True)    
class TrangThaiXetDuyet(db.Model):
    TenTrangThai = db.StringProperty(required=True)   
class NhaTuyenDung(db.Model):
    TenThuongHieu = db.StringProperty(required=True)
    MieuTa = db.TextProperty()
    Email = db.StringProperty(required=True)
    SoDienThoai = db.StringProperty(required=True)
    LinhVucID = db.ReferenceProperty(LinhVuc,required=True,collection_name='ref_LinhVuc_NTD')
    NguoiLienHe = db.StringProperty(required=True)
    QuiMoID = db.ReferenceProperty(QuiMo,required=True,collection_name='ref_QuiMo_NTD')
    AnhDaiDien = db.BlobProperty()
    Website= db.StringProperty()
    ViTri = db.GeoPtProperty()
    ViTriText = db.StringProperty()
    TrangThaiID = db.ReferenceProperty(TrangThaiXetDuyet,required=True,collection_name='ref_TrangThaiXetDuyet_NTD')
    NguoiDungID = db.IntegerProperty() 
class DoiTuong(db.Model):
    TenDoiTuong = db.StringProperty(required=True)
    MoTaNgan = db.StringProperty()
    MoTaDai = db.StringProperty()
    #NguoiDung = db.ReferenceProperty(NguoiDung,required=True,collection_name='ref_NguoiDung_DTND')
class DoiTuong_NguoiDung(db.Model):
    DoiTuongID = db.ReferenceProperty(DoiTuong,required=True,collection_name='ref_DoiTuong_DTND')
    NguoiDung = db.ReferenceProperty(NguoiDung,required=True,collection_name='ref_NguoiDung_DTND')

class DonDangTuyen(db.Model):
    NgayDang = db.DateTimeProperty(required=True)
    NgayKetThuc = db.DateTimeProperty(required=True)
    MieuTa = db.StringProperty(multiline=True,required=True)
    YeuCau = db.StringProperty(multiline=True,required=True)
    NhaTuyenDungID = db.ReferenceProperty(NhaTuyenDung,required=True,collection_name='ref_NhaTuyenDung_DDT')
    NganhNgheID = db.ReferenceProperty(NganhNghe,required=True,collection_name='ref_NganhNghe_DDT')
    ChucDanh = db.StringProperty(required=True)
    MucLuongID = db.ReferenceProperty(MucLuong,required=True,collection_name='ref_MucLuong_DDT')
    LoaiHinhID = db.ReferenceProperty(LoaiHinhCongViec,required=True,collection_name='ref_LoaiHinhCongViec_DDT')
    TinhThanhID = db.ReferenceProperty(TinhThanh,required=True,collection_name='ref_TinhThanh_DDT')
    CapBacID = db.ReferenceProperty(CapBac, required = True, collection_name = 'ref_CapBac_DDT')
    TrangThaiID = db.ReferenceProperty(TrangThaiXetDuyet,required=True,collection_name='ref_TrangThaiXetDuyet_DDT')
class NhaTuyenDung_NguoiDung(db.Model):
    NhaTuyenDungID = db.ReferenceProperty(NhaTuyenDung,required=True,collection_name='ref_NhaTuyenDung_NTDND') 
    NguoiDung  = db.ReferenceProperty(NguoiDung,required=True,collection_name='ref_NguoiDung_NTDND')

class DonDangTuyen_DoiTuong(db.Model):
    DonDangTuyenID = db.ReferenceProperty(DonDangTuyen,required=True,collection_name='ref_DonDangTuyen_DDTDT')
    DoiTuongID = db.ReferenceProperty(DoiTuong,required=True,collection_name='ref_DoiTuong_DDTDT')

def GetKey():
    vaitro = VaiTro.gql("WHERE TenVaiTro = :1", 'Admin')
    for vt in vaitro:
        vaitroID = vt.key().id()
    vaitro_admin = VaiTro.get_by_id(vaitroID)
    admin = NguoiDung.gql('WHERE VaiTro = :1', vaitro_admin)
    if(admin != None):
        for a in admin:
            key = a.key()
    return str(key)

  
def test(keyNguoiDung, TinhThanhID):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    tinhthanh_list = []
    if TinhThanhID > 0:
        tinhthanhs = TinhThanh.get_by_id(TinhThanhID)
    else:
        tinhthanhs = TinhThanh.all()
        for i in range(tinhthanhs.count()):
            tinhthanh_dict ={}
            tinhthanh_dict['TinhThanhID'] = tinhthanhs[i].key().id()
            tinhthanh_dict['TenTinhThanh'] = tinhthanhs[i].TenTinhThanh   
            tinhthanh_list.append(tinhthanh_dict)
    tuple_tinhthanh = tuple(tinhthanh_list)  
    return tuple_tinhthanh    


#---------------------------------
#- tool method
#---------------------------------
def KiemTraNguoiDung(keyNguoiDung):
    nguoidung = NguoiDung.get(db.Key(keyNguoiDung))
    if nguoidung != None:
        return True
    else:
        return False
